package com.meteor.downloader.videodownloader.clueft

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import androidx.core.app.NotificationManagerCompat
import com.meteor.downloader.videodownloader.MdmDealApplication
import com.meteor.downloader.videodownloader.mdmBaseResult64
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_CUR_NOTIFY_AD_SHOW
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_CONFIG_VALUE
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_TIME_VALUE
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_SCREEN_AA_VALUE
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_TIME_FIRST_DO
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.mMdmNotifyConfigValue
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ReceiverActionValue : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if (context != null && intent != null) {
            if (MdmDealApplication.mAppMdmOpen) return
            if (!NotificationManagerCompat.from(context)
                    .areNotificationsEnabled() || !MdmTotalUtils.getMdmAppStatus()
            ) return
            if (intent.action == Intent.ACTION_CONFIGURATION_CHANGED) {
                val orientation = context.resources.configuration.orientation
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    DataManagerMdmUtils.setDataKeyMdm(MDM_SCREEN_AA_VALUE, false)
                    return
                } else {
                    DataManagerMdmUtils.setDataKeyMdm(MDM_SCREEN_AA_VALUE, true)
                }
            }
            if (!DataManagerMdmUtils.getDataKeyMdm(MDM_SCREEN_AA_VALUE, true)) {
                return
            }
            if (mConfigIng) return
            mConfigIng = true
            getClueConfigs()
            if (!mConfigIng) return
            getFixConfig {
                if (it == null) return@getFixConfig
                CoroutineScope(Dispatchers.IO).launch {
                    mConfigIng = false
                    val clueTimeNext = DataManagerMdmUtils.getDataKeyMdm(MDM_NOTIFY_TIME_VALUE, 0L)
                    val notifyAdCount = DataManagerMdmUtils.getDataKeyMdm(MDM_CUR_NOTIFY_AD_SHOW, 1)
                    if (System.currentTimeMillis() - clueTimeNext >= if (notifyAdCount > 0) {
                            it.mdmGapNoAd * 60000L
                        } else {
                            it.mdmGap * 60000L
                        }
                    ) {
//                    if (System.currentTimeMillis() - clueTimeNext >= 10 * 1000) {
                        DataManagerMdmUtils.setDataKeyMdm(
                            MDM_NOTIFY_TIME_VALUE, System.currentTimeMillis()
                        )
                        MdmClueDownload.getTIClueShow(context, it.mdmPop, "action")
                    }
                }
            }
        }
    }

    //2500
    //43
    private var mConfigIng = false
    private val mValueClueInfosMdm = mutableListOf<ValueClueInfoMdm>()

    private fun getClueConfigs() {
        try {
            val resluta =
                DataManagerMdmUtils.getDataKeyMdm(MDM_NOTIFY_CONFIG_VALUE, mMdmNotifyConfigValue)
                    .mdmBaseResult64()
            val moshi = Moshi.Builder().build()
            val typeData = Types.newParameterizedType(
                MutableList::class.java, ValueClueInfoMdm::class.java
            )
            moshi.adapter<MutableList<ValueClueInfoMdm>>(typeData).fromJson(resluta)?.let {
                mValueClueInfosMdm.clear()
                mValueClueInfosMdm.addAll(it)
            } ?: run {
                mConfigIng = false
            }
        } catch (e: Exception) {
            mConfigIng = false
        }
    }

    private fun getFixConfig(fixData: (ValueClueInfoMdm?) -> Unit) {
        mConfigIng = try {
            val a = DataManagerMdmUtils.getDataKeyMdm(MDM_TIME_FIRST_DO, 0L)
            val data = mValueClueInfosMdm.find {
                System.currentTimeMillis() - a > it.mdmInstalled * 60000L
            }
            fixData(data)
            false
        } catch (e: Exception) {
            false
        }
    }
}